package com.houbank.demo.rabbitmq;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * Created by zhaoqiguo on 2016/9/22.
 */
@Component
public class RabbitMqListener {
    public Logger logger = LoggerFactory.getLogger(this.getClass());

   @RabbitListener(queues = AmqpConfig.QUEUE_NAME)
    public void handlerMassage(Message message){

        logger.info("######### spring.demo= "+new String(message.getBody()) );
       /*ObjectMapper objectMapper = new ObjectMapper();
       try {
           PersonDO personDO = objectMapper.readValue(new String(message.getBody()), PersonDO.class);
           logger.info(personDO.toString()+"=================");
       } catch (IOException e) {
           e.printStackTrace();
       }*/
   }

    @RabbitListener(queues = AmqpConfig.QUEUE_NAME_FAIL)
    public void handlerMessage(Message message){

        logger.info("######### spring.demo.failure = "+new String(message.getBody()) );
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String,String> map =objectMapper.readValue(new String(message.getBody()), Map.class);
            logger.info(map.get("12")+"=================");
        } catch (IOException e) {
            e.printStackTrace();
        }
       /*ObjectMapper objectMapper = new ObjectMapper();
       try {
           PersonDO personDO = objectMapper.readValue(new String(message.getBody()), PersonDO.class);
           logger.info(personDO.toString()+"=================");
       } catch (IOException e) {
           e.printStackTrace();
       }*/
    }
}
